//
//  ZTabbarController.m
//  ZPlayer
//
//  Created by Zed Link on 26/11/2016.
//  Copyright © 2016 ZED. All rights reserved.
//

#import "ZTabbarController.h"
#import "ZNavigationController.h"
#import <objc/runtime.h>
#import <objc/message.h>

@interface ZTabbarController ()

@end

@implementation ZTabbarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // ========== 添加TaabarItem ==========
    NSArray *titles = @[
                        @"首页",
                        @"动态",
                        @"工具",
                        @"我的",
                        ];
    
    NSArray *imageNames = @[
                            @"tabbar_home_nor",
                            @"tabbar_clubs_nor",
                            @"tabbar_tool_nor",
                            @"tabbar_mine_nor",
                            ];
    NSArray *highLightImageNames = @[
                                     @"tabbar_home_sel",
                                     @"tabbar_club_sel",
                                     @"tabbar_tool_sel",
                                     @"tabbar_mine_sel",
                                     ];
    
    NSArray *classNames = @[
                            @"ZHomeVC",
                            @"ZDynamicVC",
                            @"ZToolsVC",
                            @"ZMeVC",
                            ];
    
    [self setupTabbarItemrWithTitles:titles images:imageNames highlightImages:highLightImageNames andClassNames:classNames];
}

- (void)setupTabbarItemrWithTitles:(NSArray <NSString *> *)titles images:(NSArray <NSString *> *)imageNames highlightImages:(NSArray <NSString *> *)highLightImageNames andClassNames:(NSArray <NSString *> *)classNames
{
    // ========== 参数检查 ==========
    BOOL condition = (titles.count == imageNames.count && classNames.count == titles.count);
    NSString *paraErrDesc = [NSString stringWithFormat:@"%@ : TabbarItem的配置参数个数不一致", NSStringFromClass(self.class)];
    NSAssert(condition, paraErrDesc);
    
    // ========== 配置TabbarItem ==========
    NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:classNames.count];
    NSString *clsErrDesc = [NSString stringWithFormat:@"%@ : 找不到CalssName对应的类, 或该类并不是控制器", NSStringFromClass(self.class)];
    
    for (int i = 0; i < classNames.count; i++)
    {
        Class cls = NSClassFromString(classNames[i]);
        UIImage *image = [UIImage imageNamed:imageNames[i]];
        UIImage *highLightImage = [UIImage imageNamed:highLightImageNames[i]];
        
        // ========== 配置控制器 ==========
        UIViewController *vc = [[cls alloc] init];
        NSAssert([vc isKindOfClass:[UIViewController class]], clsErrDesc);
        
        // 添加Navgation
        ZNavigationController *nav = [[ZNavigationController alloc] initWithRootViewController:vc];
        
        nav.tabBarItem.title = titles[i];
        if (image == nil) {
            ZLog(@"找不到对应的图片: %@", imageNames[i]);
        } else {
            [nav.tabBarItem setImage:image];
            [nav.tabBarItem setSelectedImage:highLightImage];
        }
        
        [tempArrM addObject:nav];
    }
    
    self.viewControllers = [tempArrM copy];
}

@end
